/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.integral.service.IGaoDeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
/**
 * @author zhaozheng
 */
public class GaoDeServiceImpl implements IGaoDeService {
    private static String key = "f84ae9ba9009c49687d6e09c3e15c1fd";
    private static String configUrl = "https://restapi.amap.com/v3/config/district";
    private static String apiSearch = "https://restapi.amap.com/v3/place/around";

    @Override
    public Result configDistrict(String keywords, String subdistrict, Integer page, Integer offset, String extensions, String filter, String callback, String output) {
        StringBuilder url = new StringBuilder().append(configUrl).append("?key=").append(key);
        mosaic(url, "keywords", keywords);
        mosaic(url, "subdistrict", subdistrict);
        mosaic(url, "page", page==null?null:page.toString());
        mosaic(url, "offset", offset==null?null:offset.toString());
        mosaic(url, "extensions", extensions);
        mosaic(url, "filter", filter);
        mosaic(url, "callback", callback);
        mosaic(url, "output", output);
        String res = HttpUtil.get(url.toString());
        return Result.ok().setData(JSON.parseObject(res));
    }

    @Override
    public Result apiSearch(String location,String keywords, String types, String city, String radius, String sortrule, Integer offset, Integer page, String extensions, String sig, String output, String callback) {
        StringBuilder url = new StringBuilder().append(apiSearch).append("?key=").append(key);
        mosaic(url,"location",location);
        mosaic(url, "keywords", keywords);
        mosaic(url, "types", types);
        mosaic(url, "city", city);
        mosaic(url, "radius", radius);
        mosaic(url, "sortrule", sortrule);
        mosaic(url, "offset", offset==null?null:offset.toString());
        mosaic(url, "page", page==null?null:page.toString());
        mosaic(url, "extensions", extensions);
        mosaic(url, "output", output);
        mosaic(url, "callback", callback);
        String res = HttpUtil.get(url.toString());

        return Result.ok().setData(JSON.parseObject(res));
    }

    /**
     * url拼接参数方法
     */
    private void mosaic(StringBuilder builder, String key, String val) {
        if (StringUtils.isNotEmpty(val)) {
            builder.append("&").append(key).append("=").append(val);
        }
    }
}
